package com.cyh.forum.controller;

import com.cyh.forum.common.AppResult;
import com.cyh.forum.common.ResultCode;
import com.cyh.forum.config.AppConfig;
import com.cyh.forum.model.Message;
import com.cyh.forum.model.User;
import com.cyh.forum.services.IMessageService;
import com.cyh.forum.services.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Api(tags = "私信接口")
@RestController
@RequestMapping("/message")
public class MessageController {

    //  注入service层
    @Resource
    private IMessageService messageService;
    @Resource
    private IUserService userService;

    @ApiOperation("发送私信")
    @PostMapping("/send")
    public AppResult send (HttpServletRequest request,
                           @ApiParam("接收用户ID") @RequestParam("receiveUserId") @NonNull Long receiveUserId,
                           @ApiParam("私信内容") @RequestParam("content") @NonNull String content){
        //获取当前用户对象
        val session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        //校验当前用户是否被禁言
        if (user.getState() != 0){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED.toString());
        }
        //校验是否给用户自己发送私信
        if (user.getId() == receiveUserId){
            return AppResult.failed("不能给自己发私信");
        }

        //设置Message对象
        Message message = new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(receiveUserId);
        message.setContent(content);

        //调用service层
        messageService.create(message);

        //返回响应
        return AppResult.success();
    }


    @ApiOperation("获取私信未读数量")
    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount(HttpServletRequest request){
        //  获取当前用户
        val session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        //  调用service层
        val count = messageService.selectUnreadCount(user.getId());

        return AppResult.success(count);
    }

    @ApiOperation("获取私信列表")
    @GetMapping("/getAll")
    public AppResult<List<Message>> getAll(HttpServletRequest request){
        val session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        val messages = messageService.selectByReceiveUserId(user.getId());
        return AppResult.success(messages);
    }

    @ApiOperation("更新私信状态为已读")
    @PostMapping("/markRead")
    public AppResult markRead(HttpServletRequest request,
                              @ApiParam("私信ID") @RequestParam("id") @NonNull Long id){
        //  获取当前用户信息
        val session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //  查询message对象
        val message = messageService.selectById(id);
        //  校验用户是否为私信的接收方
        if ( message == null || user.getId() != message.getReceiveUserId()){
            //  返回响应
            return AppResult.failed("查询到了不属于自己的私信" + message.getId() + user.getId());
        }
        //  调用service层  更新为已读状态
        messageService.updateStateById(id, (byte) 1);
        //  返回结果
        return AppResult.success();
    }

    @ApiOperation("回复私信")
    @PostMapping("/reply")
    public AppResult reply(HttpServletRequest request,
                           @ApiParam("私信ID") @RequestParam("repliedId") @NonNull Long repliedId,
                           @ApiParam("回复内容") @RequestParam("content") @NonNull String content){
        //  校验当前用户的状态
        val session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState() == 1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //  校验要回复私信的状态
        val message = messageService.selectById(repliedId);
        if (message == null || message.getDeleteState() == 1  ){
            return AppResult.failed("该私信已失效");
        }
        //  校验是否是给自己的私信回复
        if (message.getPostUserId() == user.getId()){
            return AppResult.failed("不能回复自己的私信");
        }
        //  构造message对象
        Message createMessage = new Message();
        createMessage.setPostUserId(user.getId());
        createMessage.setReceiveUserId(message.getPostUserId());
        createMessage.setContent(content);
        //  调用service层
        messageService.reply(repliedId,createMessage);
        //  返回结果
        return AppResult.success();

    }



}
